<template>
  <div class="container">
    <header>
      <van-nav-bar
        title="编辑工程验收节点"
        left-arrow
        @click-left="$router.back()">
      </van-nav-bar>
    </header>
    <div class="wd-scroll-body2">
      <div class="wd-group">
        <div class="wd-group__title">基本信息</div>
        <div class="wd-group__body">
          <van-field
            v-model="form.reviewName"
            label="验收人"
            label-width="130px"
            disabled
          />
          <van-field
            v-model="form.reviewDate"
            @mousedown="ysrqShow = true"
            label="验收日期"
            label-width="130px"
            placeholder="选择验收日期"
            right-icon="clear"
            @click-right-icon="form.reviewDate = ''"
            readonly
          />
          <van-field
            v-model="form.projectName"
            @mousedown="ysxmMousedown"
            label-width="130px"
            label="验收项目"
            placeholder="选择验收项目"
            readonly
          />
          <van-field
            v-model="form.contractName"
            @mousedown="htcxMousedown"
            label-width="130px"
            label="合同名称"
            placeholder="选择合同"
            readonly
          />
          <van-field
            v-model="form.contractCode"
            label-width="130px"
            label="合同编号"
            disabled
          />
          <van-field
            label-width="130px"
            label="责任合同单位"
            v-model="form.contractUnit"
            placeholder="选择责任合同单位"
            @mousedown="zrhtdwMousedown"
            readonly
          />
          <van-field
            v-model="form.payNode"
            @mousedown="zfjdMousedown"
            label-width="130px"
            label="支付节点"
            placeholder="选择支付节点"
            readonly
          />
          <van-field
            v-model="form.buildingNames"
            label-width="130px"
            label="楼栋"
            disabled
          />
          <van-field
            v-model="form.subName"
            label-width="130px"
            label="分包合同种类"
            placeholder="选择分包合同种类"
            @mousedown="htzlMousedown"
            readonly
          />
          <van-field
            v-model="form.reviewNode"
            label-width="130px"
            label="形象进度控制节点"
            placeholder="选择节点"
            @mousedown="xxjdMousedown"
            readonly
          />
        </div>
      </div>
    </div>
    <van-row style="padding: 10px 0 20px 0">
      <van-col span="18" offset="3">
        <van-button @click="submitForm" round block type="info">保存</van-button>
      </van-col>
    </van-row>
    <!-- 验收日期选择 -->
    <van-popup
      v-model="ysrqShow"
      position="bottom">
      <van-datetime-picker
        v-model="ysrqValue"
        type="date"
        @cancel="ysrqShow = false"
        @confirm="ysrqConfirm"
      />
    </van-popup>
    <!-- 验收项目查询 -->
    <wd-search-popup
      ref="ysxmSearchPopup"
      :visible.sync="ysxmShow"
      placeholder="输入项目编号或名称查询"
      @search="ysxmSearch"
      @confirm="ysxmConfirm"
    />
    <!-- 合同查询 -->
    <wd-search-popup
      ref="htcxSearchPopup"
      :visible.sync="htcxShow"
      placeholder="输入合同名称查询"
      @search="htcxSearch"
      @confirm="htcxConfirm"
    />
    <!-- 支付节点查询 -->
    <wd-search-popup
      ref="zfjdSearchPopup"
      :visible.sync="zfjdShow"
      placeholder="输入节点名称查询"
      @search="zfjdSearch"
      @confirm="zfjdConfirm"
    />
    <!-- 责任合同选择 -->
    <van-action-sheet 
      v-model="zrhtdw.show"
      :actions="zrhtdw.options"
      @select="zrhtdwSelect"
    />
    <!-- 分包合同种类选择 -->
    <van-action-sheet 
      v-model="htzl.show"
      :actions="htzl.options"
      @select="htzlSelect"
    />
    <!-- 形象进度节点选择 -->
    <van-action-sheet 
      v-model="xxjd.show"
      :actions="xxjd.options"
      @select="mXxjdSelect"
    />
  </div>
</template>
<script>
  import searchPopup from '@/components/searchPopup.vue'
  import moment from 'moment'
  import {
    apiGetUserProject,
    apiGetPayNodeList,
    apiGetPMContractInfo,
    apiGetBuilingList,
    apiGetSubpackageList,
    apiPostBaseInfo
  } from '@/api/'

  const userNo = localStorage.getItem('userID')
  const editBaseData = JSON.parse(localStorage.getItem('editBaseData'))

  export default {
    components: {
      'wdSearchPopup': searchPopup
    },
    data() {
      return {
        htcxShow: false,
        ysxmShow: false,
        ysrqShow: false,
        zfjdShow: false,
        projectCompany: '',
        zrhtdw: {
          show: false,
          options: []
        },
        htzl: {
          show: false,
          options: []
        },
        xxjd: {
          show: false,
          options: []
        },
        ysrqValue: new Date(),
        form: {
          reviewName: '',
          reviewDate: '',
          projectCode: '',
          projectName: '',
          contractCode: '',
          contractName: '',
          contractUnit: '',
          payNodeCode: '',
          payNode: '',
          buildingCodes: '',
          buildingNames: '',
          subId: '',
          subName: '',
          reviewNodeId: '',
          reviewNode: ''
        }
      }
    },
    created() {
      const
        vm = this,
        form = vm.form,
        data = editBaseData

      for (let p in form) form[p] = data[p.toUpperCase()]

      // 转换日期格式
      if (data.REVIEWDATE) {
        let time = data.REVIEWDATE.time
        vm.ysrqValue = new Date(time)
        form.reviewDate = moment(time).format('YYYY-MM-DD')
      }

      this.projectCompany = data.COMPANYCODE
      this.form.subId = data.SUBID
      this.form.subName = data.SUBPACKAGE
      this.form.reviewNode = data.FIGURENODE
      this.form.buildingNames = data.BUILDING
      this.form.payNodeNo = data.PAYNODECODE
    },
    mounted() {
      // 提前加载下拉数据
      this.ysxmSearch('')
      this.htcxSearch('')
      this.zfjdSearch('')
    },
    methods: {
      // 选中形象进度
      mXxjdSelect(obj) {
        this.form.reviewNodeId = obj.value
        this.form.reviewNode = obj.name
        this.xxjd.show = false
      },
      // 确定选中验收日期
      ysrqConfirm() {
        this.form.reviewDate = moment(this.ysrqValue).format('YYYY-MM-DD')
        this.ysrqShow = false
      },
      // 弹出验收项目查询
      ysxmMousedown() {
        this.ysxmShow = true
      },
      // 查询验收项目
      ysxmSearch(val) {
        const
          vm = this,
          list = [],
          searchPopup = vm.$refs.ysxmSearchPopup

        searchPopup.loading = true

        apiGetUserProject(userNo, val).then(res => {
          if (res.data) {
            res.data.forEach(item => {
              list.push({
                label: item.projectName,
                value: item.projectCode,
                company: item.projectCompany
              })
            })
          } else {
            this.$toast('获取项目出错！')
          }
          searchPopup.dropdownList = list
          searchPopup.loading = false
        })
      },
      // 确定选中验收项目
      ysxmConfirm(obj) {
        this.ysxmShow = false

        if (obj.value !== this.form.projectCode) {
          this.$dialog.confirm({
            titile: '提示',
            message: '项目信息修改后，后续数据将会重置清空，请确认是否修改？'
          }).then(() => {
            this.form.projectCode = obj.value
            this.form.projectName = obj.label
            this.projectCompany = obj.company
            this.form.contractCode = ''
            this.form.contractName = ''
            this.form.contractUnit = ''
            this.form.payNode = ''
            this.form.payNodeCode = ''
            this.form.buildingNames = ''
            this.form.buildingCodes = ''
            this.form.subId = ''
            this.form.subName = ''
            this.form.reviewNode = ''
            this.form.reviewNodeId = ''
            this.$refs.htcxSearchPopup.dropdownList = []
            this.$refs.zfjdSearchPopup.dropdownList = []
            this.htcxSearch('')
          }).catch(err => {})
        }
      },
      // 弹出合同查询框
      htcxMousedown() {
        if (!this.form.projectCode) {
          this.$toast('请先选择项目')
          return
        }
        this.htcxShow = true
      },
      // 查询合同
      htcxSearch(val) {
        const
          vm = this,
          list = [],
          searchPopup = vm.$refs.htcxSearchPopup

        searchPopup.loading = true

        apiGetPMContractInfo(userNo, vm.form.projectCode, vm.projectCompany, val).then(res => {
          if (res.data) {
            res.data.forEach(item => {
              list.push({
                label: item.contractName,
                value: item.contractPlanCodeLS,
                signerinfor: item.contractSignerinfor
              })
            })
          } else {
            this.$toast('获取合同出错！')
          }
          searchPopup.dropdownList = list
          searchPopup.loading = false
        })
      },
      // 确定选中合同
      htcxConfirm(obj) {
        this.htcxShow = false
        if (obj.value !== this.form.contractCode) {
          this.$dialog.confirm({
            titile: '提示',
            message: '合同信息修改后，后续数据将会重置清空，请确认是否修改？'
          }).then(() => {
            this.form.contractCode = obj.value
            this.form.contractName = obj.label
            const opts = []
            JSON.parse(obj.signerinfor).SignerInfor.forEach(item => {
              if (item.signer_type != '甲方') {
                opts.push({
                  name: item.company_name,
                  value: item.company_name
                })
              }
            })
            this.zrhtdw.options = opts
            this.form.contractUnit = ''
            this.form.payNode = ''
            this.form.payNodeCode = ''
            this.form.buildingNames = ''
            this.form.buildingCodes = ''
            this.form.subId = ''
            this.form.subName = ''
            this.form.reviewNode = ''
            this.form.reviewNodeId = ''
            this.zfjdSearch('')
          })
        }
      },
      // 弹出责任合同选择框
      zrhtdwMousedown() {
        if (!this.form.contractCode) {
          this.$toast('请先选择合同')
          return
        }
        if (this.zrhtdw.options.length < 1) return
        this.zrhtdw.show = true
      },
      // 选中责任合同
      zrhtdwSelect(obj) {
        this.form.contractUnit = obj.value
        this.zrhtdw.show = false
      },
      // 弹出支付节点查询框
      zfjdMousedown() {
        if (!this.form.projectCode || !this.form.contractCode) {
          this.$toast('请先选择项目和合同')
          return
        }
        this.zfjdShow = true
      },
      // 查询支付节点
      zfjdSearch(val) {
        const
          vm = this,
          list = [],
          searchPopup = vm.$refs.zfjdSearchPopup

        searchPopup.loading = true

        apiGetPayNodeList(userNo, vm.form.projectCode, vm.form.contractCode, val).then(res => {
          if (res.data) {
            res.data.forEach(item => {
              list.push({
                label: item.ZHIFUJIEDIANMINGCHENG,
                value: item.ZHIFUJIEDIANBIANHAO,
                DUIXIANGID: item.DUIXIANGID,
                FANBENBIANHAO: item.FANBENBIANHAO
              })
            })
          } else {
            this.$toast('获取支付节点数据出错！')
          }
          searchPopup.dropdownList = list
          searchPopup.loading = false
        })
      },
      // 确认选中支付节点
      zfjdConfirm(obj) {
        const vm = this

        vm.form.payNodeNo = obj.value
        vm.form.payNode = obj.label
        vm.zfjdShow = false

        // 获取楼栋
        apiGetBuilingList(userNo, vm.form.projectCode, vm.form.contractCode, obj.DUIXIANGID).then(res => {
          if (res.data) {
            let
              names = [],
              codes = []

            res.data.forEach(item => {
              names.push(item.DUIXIANGMINGCHENG)
              codes.push(item.DUIXIANGID)
            })

            vm.form.buildingNames = names.join(',')
            vm.form.buildingCodes = codes.join(',')
          } else {
            vm.$toast('获取楼栋数据出错！')
          }
        })

        // 获取分包合同种类
        apiGetSubpackageList(userNo, obj.FANBENBIANHAO).then(res => {
          if (res.data) {
            let
              fbOps = [],
              jdOps = []

            res.data.forEach(item => {
              fbOps.push({
                name: item.SUBNAME,
                value: item.SUBID
              })
              jdOps.push({
                name: item.NODENAME,
                value: item.NODEID
              })
            })

            vm.htzl.options = fbOps
            vm.xxjd.options = jdOps
          } else {
            vm.$toast('获取分包合同种类出错！')
          }
        })
      },
      // 弹出分包合同种类选择框
      htzlMousedown() {
        if (this.htzl.options.length < 1) return
        if (!this.form.payNodeNo) {
          this.$toast('请先选择支付节点')
          return
        }
        this.htzl.show = true
      },
      // 选中分包合同种类
      htzlSelect(obj) {
        this.form.subId = obj.value
        this.form.subName = obj.name
        this.htzl.show = false
      },
      // 弹出形象进度控制节点
      xxjdMousedown() {
        if (this.xxjd.options.length < 1) return
        if (!this.form.subId) {
          this.$toast('请先选择分包合同种类')
          return
        }
        this.xxjd.show = true
      },
      // 提交表单
      submitForm() {
        if (!this.form.reviewNodeId) {
          this.$toast('信息填写不完整！')
          return
        }

        apiPostBaseInfo(userNo, this.form).then(res => {
          if (res.data) {
            localStorage.setItem('addedID', editBaseData.ID)
            this.$router.push('/engNodeAcpt/editremain')
          } else {
            this.$toast('新增失败，请重试！')
          }
        })
      }
    }
  }
</script>